/**
 * @Author:  renyc
 * @Date: 2023年2月9日10:14:55
 */
'use strict';

// import * as Cesium from "cesium/Cesium";
import SourceType from "../../type/SourceType";
import OgcProviderFactory from "../../provider/ogc/OgcProviderFactory";
import WFSVersion from "../../provider/ogc/wfs/WFSVersion";
import WFSProvider from "../../provider/ogc/wfs/WFSProvider";
import { SourceEventType } from "../../../event/EventType";
import DataFormat from "../../type/DataFormat";
import FeatureSource from "../vector/FeatureSource";

// VECTOR_SOURCE_OPTS = {
//     SOUCE_OPTS,
//     url :  
// }
class WebFeatureSource extends FeatureSource {

    /**
     * WFS版本
     */
    _vesion = WFSVersion.V_1_1_0;
    _outputFormat = "application/json";
    

    _url = undefined;           //WFS的URL地址
    _typeName   = undefined;    //WFS的数据集名称
    _filter     = undefined;    //WFS的filter
    _fields     = [];           //WFS返回的字段列表    

    // _delegate = undefined;      //针对Cesium，WFS数据源获取的数据，以Entity的方式缓存以便Cesium进行可视化。

    constructor(name, options){

        options = options || {};
        super(name, options);        
        
        this._type = SourceType.WFS;
        if(options){
            if(options.version){
                this._version = options.version;
            }
        }
        this._provider = OgcProviderFactory.createWfsProvider(this._vesion);
        if(!this._provider){
            throw "不支持的WFS版本";
        }

        //应用options参数
        this._url  = options.url;
        this._typeName = options.typeName;
        this._fields = options.fields;
        this._filter = options.filter;
        //this._delegate = new Cesium.CustomDataSource(this._id);


        // this._json = undefined;         //GeoJSON对象，缓存fetch的数据
        // this._delegate = null;          
        // this._cesiumGeoJsonDataSource = new Cesium.GeoJsonDataSource(this._id);
    }

    /**
     * 获取Json数据
     */
    get json(){
        return this._json;
    }

    /**
     * 返回内部delegate属性，内部delegate是Cesium对象
     */
    get delegate(){
        return this._delegate;
    }

    /**
     * 清空Source
     * @override
     */
    cleanup(){
        if(this._delegate){
            this._delegate.then(dataSource=>{
                dataSource.entities.removeAll();
            })
        }
    }

    /**
     * 发送Web请求，执行查询，从数据源获取数据
     */
    //query(typeName, query){
    fetch(){        
        let that = this;
        let typeName = this._typeName;
        let fields = this._fields;
        let filter = this._filter;

        if(!typeName){
            this._fire(SourceEventType.FETCH_ERROR, { 
                message: "未定义typeName参数"
            });            
        }

        //1. 生成query的XML文档
        let provider = this._provider;//OgcProviderFactory.createWfsProvider(this._vesion);
        let body = provider.encodeGetFeature(typeName, fields, filter);

        fetch(this._url,{
            method: 'post',
            body: body,
            headers: {
                'Content-Type': 'application/xml'
            }
        }).then(resp=>{
            if(resp.status!== 200){
                //触发FETCH_ERROR事件
                this._fire(SourceEventType.FETCH_ERROR, { 
                    message: resp.statusText
                });
                return;
            }
            else{
                return resp.json();
            }            
        })
        .then(json=>{
            if(json){
                that._onFetchedEvent(json);
                // //触发数据获取的FETCHED事件
                // this._fire(SourceEventType.FETCHED, { 
                //     data: json,
                //     format : DataFormat.GEOJSON,
                //     source : that,
                //     datasetName : typeName
                // });
            }
            
        })
        .catch(err=>{
            //触发FETCH_ERROR事件
            this._fire(SourceEventType.FETCH_ERROR, { 
                message: err
            });
        });
    }

    _onFetchedEvent(json){
        let that = this;
        this._json = json;

        //触发数据获取的FETCHED事件
        that._fire(SourceEventType.FETCHED, { 
            json : json,            
            source : that,
            // format : DataFormat.GEOJSON,
            // datasetName : typeName
        });

        //this._delegate = this._cesiumGeoJsonDataSource.load(json, { clampToGround:true });

        // this._cesiumGeoJsonDataSource.load(json, { clampToGround:true }).then(data => {
        //     alert("dsafsfasfs");
        // });



        // //触发数据获取的FETCHED事件
        // this._fire(SourceEventType.FETCHED, { 
        //     json: json,
        //     data: this._delegate,                
        //     source : that,
        //     // format : DataFormat.GEOJSON,
        //     // datasetName : typeName
        // });
    }

    _createEntity(feature){

    }
}

export default WebFeatureSource;